﻿using System;
using System.Collections.Generic;

namespace Chimp.Core
{
    public class Create : JointNode
    {
        public Create(string t, JointNode p)
        {
            CurType = TypeName(t);
            Expr = new List<JointNode>();
            SetData(p);
        }

        public string CurType { get; set; }
        public List<JointNode> Expr { get; set; }

        public override void WriteOutput()
        {
            if (Expr.Count == 0)
            {
                _cw.Write("new " + CurType + "()");
            }
            else
            {
                _cw.Write("new " + CurType + "(");
                int i = 0;
                Console.WriteLine("Writing " + Expr.Count + " Expressions.");
                foreach (JointNode r in Expr)
                {
                    r.WriteOutput();
                    if (i < Expr.Count - 1)
                    {
                        _cw.Write(",");
                    }
                    i++;
                }
                _cw.Write(")");
            }
        }

        public override void ParseNode()
        {
            if (PeekTok() == "(")
            {
                GetTok();

                while (!Eof)
                {
                    Expr.Add(ParseExpr());
                    if (PeekTok() == ",")
                    {
                        GetTok();
                        continue;
                    }
                    if (PeekTok() == ")")
                    {
                        GetTok();
                        return;
                    }
                }
            }
        }
    }
}